package com.technology.education.ui.common.video_play

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import cn.jzvd.Jzvd
import com.angcyo.dsladapter.*
import com.technology.education.R
import com.technology.education.databinding.ActivityVideoPlayBinding
import com.technology.education.dslitem.AppStatusItem
import com.technology.education.dslitem.ItemSingleImage
import com.technology.education.dslitem.ItemSingleText
import com.technology.education.ext.showLog
import me.hgj.jetpackmvvm.base.activity.BaseVmDbActivity
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

/**
 *@author:create by BZY
 *Date: 2022/4/1 08
 */
class VideoPlayActivity : BaseVmDbActivity<BaseViewModel, ActivityVideoPlayBinding>() {
    /**提供基本的分割线功能*/
    var baseDslItemDecoration = DslItemDecoration()

    var myAdapter: DslAdapter = DslAdapter().apply {
        dslAdapterStatusItem = AppStatusItem().apply {
            itemSpanCount = -1
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        intent.apply {
            val url = getStringExtra("url") ?: ""
            showLog("playVideo----$url")
            playVideo(url, getStringExtra("title") ?: "")
            setUI(getStringExtra("answer"), getStringArrayListExtra("image"))
        }
        mDatabind.jzVideo.watchBack = {
            onBackPressed()
        }
        initRv()
    }

    private fun setUI(answer: String?, images: ArrayList<String>?) {
        if (answer.isNullOrEmpty() && images.isNullOrEmpty()) {
            myAdapter.render {
                updateAdapterStatus(DslAdapterStatusItem.ADAPTER_STATUS_EMPTY)
            }
        } else {
            myAdapter.render {
                //辅导笔记展示
                dslItem(ItemSingleText("辅导笔记", itemSpan = -1)) {
                    itemSpanCount = -1
                    style.lineHeight = 0
                    style.bottomPadding = 0
                    style.isBold = true
                }
                dslItem(ItemSingleText(answer)) {
                    itemSpan = -1
                    itemSpanCount = -1
                    itemLeftInsert = 12 * dpi
                    itemRightInsert = 12 * dpi
                    style.lineColor = R.color.white
                    style.topPadding = 10
                    style.bottomPadding = 10
                    style.leftPadding = 12
                    style.rightPadding = 12
                    style.itemBackDrawable = R.drawable.round_gray
                    style.itemBackColor = 0
                    style.lineHeight = 0
                }

                if (!images.isNullOrEmpty()) {
                    images.forEach {
                        dslItem(ItemSingleImage(it, size = 80*dpi)) {
                            itemSpanCount = 1
                        }
                    }
                }
            }
        }
    }

    private fun playVideo(url: String, title: String) {
        mDatabind.jzVideo.setUp(
            url,
            title
        )
        mDatabind.jzVideo.startVideoAfterPreloading()
        myAdapter.updateItemDepend()
    }

    private fun initRv() {
        mDatabind.rvList?.apply {
            addItemDecoration(baseDslItemDecoration)
            adapter = myAdapter
            layoutManager = GridLayoutManager(context, 4).apply {
                dslSpanSizeLookup(myAdapter)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        Jzvd.releaseAllVideos()
    }

    override fun onBackPressed() {
        if (Jzvd.backPress()) {
            return
        }
        super.onBackPressed()
    }


    override fun showLoading(message: String) {
    }

    override fun dismissLoading() {
    }

    override fun createObserver() {
    }
}